import { Component, OnInit } from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {TipService} from "../../services/tip.service";
import {ExtendHttpService} from "../../services/extend-http.service";
import Common from '../../common/common';

@Component({
  selector: 'app-forgot-pwd-step2',
  templateUrl: './forgot-pwd-step2.component.html',
  styleUrls: ['./forgot-pwd-step2.component.css']
})
export class ForgotPwdStep2Component implements OnInit {

  isLoading = false;
  tipType = 'error';
  info = {mobile: '', code: '', newPwd: '', oldNew:''};

  constructor(private activatedRoute: ActivatedRoute,
              private tipService: TipService,
              private extendHttp: ExtendHttpService,
              private router: Router) {
    this.activatedRoute.queryParams.subscribe(res => {
      this.info.mobile = res['mobile'];
      this.info.code = res['code'];
    })
  }

  ngOnInit() {
    cBack =()=> {
      $('#btn-back').trigger('click');
    }
  }

  onSubmit() {
    if (!this.info.newPwd.trim()) {
      this.tipService.change(true, '请输入密码');
      return;
    }
    if (!this.info.oldNew.trim()) {
      this.tipService.change(true, '请再次输入新密码');
      return;
    }
    if (this.info.newPwd.trim() != this.info.oldNew.trim()) {
      this.tipService.change(true, '两次密码输入不一致');
      return;
    }
    this.info.newPwd = hex_md5(this.info.newPwd);

    this.isLoading = !this.isLoading;
    this.extendHttp.post('/agentApp/login/updatePwd', this.info).subscribe(response => {
      let res = response.json();
      if (res.code == '0') {
        this.tipType = 'success';
        this.isLoading = !this.isLoading;
        this.tipService.change(true, res['message']);
        setTimeout(()=>{
          this.router.navigate(['/login']);
        }, 1500);
      } else {
        this.tipService.change(true, res['message']);
        this.isLoading = !this.isLoading;
      }
    }, error => {
      this.tipService.change(true, '系统繁忙，操作失败！');
      this.isLoading = !this.isLoading;
    });
  }

}
